package gui;

import houseHold.GHObject;
import houseHold.House;
import houseHold.HouseHoldObject;
import houseHold.ObjectType;
import houseHold.Room;
import houseHold.electricObjects.ElectricObject;
import houseHold.electricObjects.ElectricObjectType;
import houseHold.gasObjects.GasObjectType;
import houseHold.waterObjects.WaterObject;
import houseHold.waterObjects.WaterObjectType;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.SystemColor;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;

import sensor.Sensor;
import sensor.SensorType;
import consumption.ConsumptionType;

public class AddObject extends JDialog {

	private static final long serialVersionUID = 1L;

	public static void main(String args[]) {
		Room callingRoom = new Room();
		Room c2 = new Room();
		c2.setName("r2");
		HouseHoldObject callingObject = new ElectricObject();
		HouseHoldObject callingObject2 = new WaterObject();
		callingObject.setLocation(callingRoom);
		callingObject2.setLocation(callingRoom);
		callingRoom.addItem(callingObject);
		callingRoom.addItem(callingObject2);
		House.getInstance().addRoom(callingRoom);
		House.getInstance().addRoom(c2);
		Sensor s1, s2, s3;
		s1 = new Sensor();
		s1.setType(SensorType.ELECTRIC_SENSOR);
		s2 = new Sensor();
		s2.setType(SensorType.WATER_SENSOR);
		s3 = new Sensor();
		s3.setType(SensorType.GAS_SENSOR);
		callingObject.setSensor(s1);
		callingObject2.setSensor(s2);
		callingRoom.addSensor(s1);
		callingRoom.addSensor(s2);
		// AddObject ao=new AddObject(new JFrame());
		// //ao.initialize(callingRoom);
		// ao.setVisible(true);
	}

	private JButton Add = null;
	private JPanel AddObject = null;
	private Room callingRoom = null;
	private JButton Cancel = null;
	private JLabel Consumption = null;
	private JTextField ConsumptionField = null;
	private JComboBox ConsumptionTypeCBox = null;
	private JLabel ConsumptionTypeLabel = null;
	private JLabel ErrorLabel = null;
	private JPanel jContentPane = null;
	private RoomInformationDialog jd;
	private JTextField LocationField = null;
	private JTextField NameField = null;
	private HouseHoldObject newObject = null; // @jve:decl-index=0:
	private JLabel ObjectLocation = null;
	private JLabel ObjectName = null;
	private JLabel ObjectType = null;
	private JComboBox ObjectTypeCBox = null;
	private JLabel Sensor = null;
	private JComboBox SensorCBox = null;

	private boolean startUp = true;

	/**
	 * @param owner
	 */
	public AddObject(Frame owner, RoomInformationDialog jd) {
		super(owner);
		this.jd = jd;

	}

	/**
	 * This method initializes Add
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAdd() {
		if (Add == null) {
			Add = new JButton();
			Add.setBounds(new Rectangle(220, 200, 130, 25));
			Add.setText("Add");
			Add.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					callingRoom.addItem(newObject);
					jd.setEnabled(true);
					jd.populateLists();
					getDialog().setVisible(false);
				}
			});
		}
		return Add;
	}

	/**
	 * This method initializes AddObject
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getAddObject() {
		if (AddObject == null) {
			ErrorLabel = new JLabel();
			ErrorLabel.setBounds(new Rectangle(20, 142, 470, 25));
			ErrorLabel.setForeground(new Color(196, 0, 0));
			ErrorLabel.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC,
					13));
			ErrorLabel.setHorizontalAlignment(SwingConstants.CENTER);
			ErrorLabel.setText("");
			ObjectLocation = new JLabel();
			ObjectLocation.setBounds(new Rectangle(250, 30, 100, 25));
			ObjectLocation.setHorizontalAlignment(SwingConstants.RIGHT);
			ObjectLocation.setFont(new Font("SansSerif", Font.BOLD, 12));
			ObjectLocation.setText("Location:");
			Sensor = new JLabel();
			Sensor.setBounds(new Rectangle(250, 110, 100, 25));
			Sensor.setHorizontalAlignment(SwingConstants.RIGHT);
			Sensor.setFont(new Font("SansSerif", Font.BOLD, 12));
			Sensor.setText("Sensor:");
			Consumption = new JLabel();
			Consumption.setBounds(new Rectangle(250, 70, 100, 25));
			Consumption.setHorizontalAlignment(SwingConstants.RIGHT);
			Consumption.setFont(new Font("SansSerif", Font.BOLD, 12));
			Consumption.setText("Consumption:");
			ObjectType = new JLabel();
			ObjectType.setBounds(new Rectangle(0, 110, 120, 25));
			ObjectType.setHorizontalAlignment(SwingConstants.RIGHT);
			ObjectType.setFont(new Font("SansSerif", Font.BOLD, 12));
			ObjectType.setText("Object type:");
			ConsumptionTypeLabel = new JLabel();
			ConsumptionTypeLabel.setBounds(new Rectangle(0, 70, 120, 25));
			ConsumptionTypeLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			ConsumptionTypeLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
			ConsumptionTypeLabel.setText("Consumption type:");
			ObjectName = new JLabel();
			ObjectName.setBounds(new Rectangle(0, 30, 120, 25));
			ObjectName.setHorizontalAlignment(SwingConstants.RIGHT);
			ObjectName.setFont(new Font("SansSerif", Font.BOLD, 12));
			ObjectName.setText("Name:");
			AddObject = new JPanel();
			AddObject.setLayout(null);
			AddObject.setBounds(new Rectangle(10, 10, 511, 180));
			AddObject.setBackground(new Color(245, 245, 245));
			AddObject.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory.createTitledBorder(BorderFactory
							.createLineBorder(Color.gray, 5), "Add Object",
							TitledBorder.DEFAULT_JUSTIFICATION,
							TitledBorder.DEFAULT_POSITION, new Font(
									"SansSerif", Font.BOLD, 14),
							SystemColor.textHighlight)));
			AddObject.add(ObjectName, null);
			AddObject.add(ConsumptionTypeLabel, null);
			AddObject.add(ObjectType, null);
			AddObject.add(Consumption, null);
			AddObject.add(Sensor, null);
			AddObject.add(ObjectLocation, null);
			AddObject.add(getNameField(), null);
			AddObject.add(getLocationField(), null);
			AddObject.add(getConsumptionField(), null);
			AddObject.add(getConsumptionTypeCBox(), null);
			AddObject.add(getObjectTypeCBox(), null);
			AddObject.add(getSensorCBox(), null);
			AddObject.add(ErrorLabel, null);

		}
		return AddObject;
	}

	private JButton getCancel() {
		if (Cancel == null) {
			Cancel = new JButton();
			Cancel.setBounds(new Rectangle(380, 200, 130, 25));
			Cancel.setText("Cancel");
			Cancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jd.setEnabled(true);
					getDialog().setVisible(false);
				}
			});
		}
		return Cancel;
	}

	/**
	 * This method initializes ConsumptionField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getConsumptionField() {
		if (ConsumptionField == null) {
			ConsumptionField = new JTextField();
			ConsumptionField.setBounds(new Rectangle(360, 70, 130, 25));
			ConsumptionField.addKeyListener(new java.awt.event.KeyAdapter() {
				@Override
				public void keyReleased(java.awt.event.KeyEvent e) {

					try {
						newObject.setConsumption(Integer
								.parseInt(ConsumptionField.getText()));
						writeError("");
					} catch (Exception ee) {
						writeError("Invalid Consumption value");
					}
					System.out.println(newObject);
				}

			});
		}
		return ConsumptionField;
	}

	/**
	 * This method initializes ConsumptionTypeCBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getConsumptionTypeCBox() {
		if (ConsumptionTypeCBox == null) {
			ConsumptionTypeCBox = new JComboBox();
			ConsumptionTypeCBox.setBounds(new Rectangle(130, 70, 135, 25));
			ConsumptionTypeCBox.setBackground(new Color(255, 255, 245));
			ConsumptionTypeCBox
					.addItemListener(new java.awt.event.ItemListener() {
						public void itemStateChanged(java.awt.event.ItemEvent e) {
							if (!startUp)
								if (ConsumptionTypeCBox.getSelectedIndex() != (-1)) {
									newObject = newObject
											.modifyConsumptionType(ConsumptionType
													.match((String) ConsumptionTypeCBox
															.getSelectedItem()));
									InitOtherFields(newObject
											.getConsumptionType());
								}
							startUp = false;
						}

					});
		}
		return ConsumptionTypeCBox;
	}

	/**
	 * This method initializes Cancel
	 * 
	 * @return javax.swing.JButton
	 */

	private JDialog getDialog() {
		return this;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getAddObject(), null);
			jContentPane.add(getAdd(), null);
			jContentPane.add(getCancel(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes LocationField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getLocationField() {
		if (LocationField == null) {
			LocationField = new JTextField();
			LocationField.setForeground(new Color(150, 150, 150));
			LocationField.setEditable(false);
			LocationField.setBackground(Color.white);
			LocationField.setBounds(new Rectangle(360, 30, 130, 25));
		}
		return LocationField;
	}

	/**
	 * This method initializes NameField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getNameField() {
		if (NameField == null) {
			NameField = new JTextField();
			NameField.setBounds(new Rectangle(130, 30, 135, 25));
			NameField.setBackground(Color.white);
			NameField.setForeground(new Color(150, 150, 150));
			NameField.setEditable(false);
		}
		return NameField;
	}

	/**
	 * This method initializes ObjectTypeCBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getObjectTypeCBox() {
		if (ObjectTypeCBox == null) {
			ObjectTypeCBox = new JComboBox();
			ObjectTypeCBox.setBounds(new Rectangle(130, 110, 135, 25));
			ObjectTypeCBox.setBackground(new Color(255, 255, 245));
			ObjectTypeCBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					setType();

				}
			});
		}
		return ObjectTypeCBox;
	}

	/**
	 * This method initializes SensorCBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getSensorCBox() {
		if (SensorCBox == null) {
			SensorCBox = new JComboBox();
			SensorCBox.setBounds(new Rectangle(360, 110, 130, 25));
			SensorCBox.setBackground(new Color(255, 255, 245));
			SensorCBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					setSensor();
				}
			});
		}
		return SensorCBox;
	}

	private void initCTypeCBox() {
		boolean flag = true;
		if (callingRoom.hasGasSensor()) {
			ConsumptionTypeCBox.addItem("gas");
			flag = false;
		}
		if (callingRoom.hasWaterSensor()) {
			ConsumptionTypeCBox.addItem("water");
			flag = false;
		}
		if (callingRoom.hasElectricSensor()) {
			ConsumptionTypeCBox.addItem("electricity");
			flag = false;
		}
		if (flag)
			writeError("Error: Room has no sensors!");
		else {
			ConsumptionTypeCBox.setVisible(true);
			ConsumptionTypeLabel.setVisible(true);
			startUp = true;
			ConsumptionTypeCBox.setSelectedIndex(-1);

		}
	}

	private void initFields() {
		newObject = new HouseHoldObject(true);
		NameField.setText(newObject.getName());
		LocationField.setText(callingRoom.getName());
		ConsumptionField.setText("0");
		Consumption.setVisible(false);
		ConsumptionField.setVisible(false);
		ObjectType.setVisible(false);
		ObjectTypeCBox.setVisible(false);
		Sensor.setVisible(false);
		SensorCBox.setVisible(false);
		ConsumptionTypeLabel.setVisible(false);
		ConsumptionTypeCBox.setVisible(false);
		writeError("");
		initCTypeCBox();
		Add.setEnabled(false);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	public void initialize(Room cRoom) {
		this.setSize(545, 275);
		this.setContentPane(getJContentPane());
		this.setTitle("Add a new Object");
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent e) {
				jd.setEnabled(true);
			}
		});
		callingRoom = cRoom;
		initFields();

	}

	private void InitOtherFields(ConsumptionType consumptionType) {
		Add.setEnabled(true);
		Consumption.setVisible(true);
		ConsumptionField.setVisible(true);
		ComboBoxModel objectModel = new DefaultComboBoxModel();
		switch (consumptionType) {
		case GAS:
			objectModel = new DefaultComboBoxModel(GasObjectType.GAS_OBJECT
					.getList());
			break;
		case WATER:
			objectModel = new DefaultComboBoxModel(WaterObjectType.WATER_OBJECT
					.getList());
			break;
		case ELECTRICITY:
			objectModel = new DefaultComboBoxModel(
					ElectricObjectType.ELECTRIC_OBJECT.getList());
			break;
		default:
			break;
		}
		ObjectTypeCBox.setModel(objectModel);
		ObjectType.setVisible(true);
		ObjectTypeCBox.setVisible(true);
		ObjectTypeCBox.setSelectedIndex(0);
		ComboBoxModel sensorModel = new DefaultComboBoxModel(callingRoom
				.getSensorList(consumptionType));
		SensorCBox.setModel(sensorModel);
		Sensor.setVisible(true);
		SensorCBox.setVisible(true);
		SensorCBox.setSelectedIndex(0);
		setType();
		setSensor();
	}

	private void setSensor() {
		newObject.setSensor((Sensor) SensorCBox.getSelectedItem());
	}

	private void setType() {
		ObjectType oType;
		switch (newObject.getConsumptionType()) {
		case GAS:
			oType = GasObjectType.GAS_OBJECT;
			break;
		case WATER:
			oType = WaterObjectType.WATER_OBJECT;
			break;
		case ELECTRICITY:
			oType = ElectricObjectType.ELECTRIC_OBJECT;
			break;
		default:
			oType = GHObject.OBJECT;
			break;
		}
		oType = oType.match((String) ObjectTypeCBox.getSelectedItem());
		if (oType == null)
			oType = GHObject.OBJECT;
		newObject.setType(oType, false);
		getNameField().setText(newObject.getName());
	}

	public void writeError(String s) {
		if (s.length() > 0) {
			ErrorLabel.setText(s);
			Add.setEnabled(false);
		} else {
			Add.setEnabled(true);
			ErrorLabel.setText("");
		}
	}

} // @jve:decl-index=0:visual-constraint="10,10"
